<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Reservasi extends Authenticated_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('reservasi_model', 'reser');
		$this->load->model('prosentase_charge_model', 'pros');
		$this->load->model('spbu_model', 'spbu');
		$this->load->model('coa_model', 'coa');
		$this->load->model('setoran_kasir_model', 'kasir');
		$this->load->model('biaya_model', 'biaya');
	}

	function index() {
		$this->list_reservasi();
	}

	function list_reservasi($tgl='') {
                if($this->session->userdata('level')!=4)
                {
                    $data['daftar_asal_tujuan'] = $this->reser->get_asal_tujuan();
                    $data['list'] = $this->reser->get_all($tgl);
                } else
                {
                    $data['daftar_asal_tujuan'] = $this->reser->get_asal_tujuan();
                    $data['daftar_tujuan'] = $this->reser->get_asal_tujuan_for_kasir($this->session->userdata('kota'));
                    $data['list'] = $this->reser->get_all_for_kasir_tujuan($this->session->userdata('kota'),$tgl);
                }
                if ($tgl == '') {
			$data['tgl'] = date('m/d/Y');
		} else {
			$data['tgl'] = $this->date_func->change_format_date($tgl, 'd-m-Y', 'm/d/Y');
		}

		$this->load->view('template/header_view');
		$this->load->view('reservasi/daftar_reservasi', $data);
		$this->load->view('template/footer_view');
	}

	function list_pembayaran_operasional_awal($tgl='') {
		if($this->session->userdata('level')!=4){
			$data['daftar_asal_tujuan'] = $this->reser->get_asal_tujuan();
			$data['list'] = $this->reser->get_all($tgl);
		}else{
			$data['daftar_asal_tujuan'] = $this->reser->get_asal_tujuan();
			$data['daftar_asal'] = $this->reser->get_asal_tujuan_for_kasir($this->session->userdata('kota'));
			$data['list'] = $this->reser->get_all_for_kasir_asal($this->session->userdata('kota'),$tgl);
		}
		if ($tgl == '') {
			$data['tgl'] = date('m/d/Y');
		} else {
			$data['tgl'] = $this->date_func->change_format_date($tgl, 'd-m-Y', 'm/d/Y');
		}

		$this->load->view('template/header_view');
		$this->load->view('reservasi/daftar_pembayaran_operasional_awal', $data);
		$this->load->view('template/footer_view');
	}

	function cetak_op_awal($noSPJ){
                $data['nospj']=$noSPJ;
		$data['reservasi'] = $this->reser->get_one($noSPJ);
		$data['subtotal'] = $this->reser->get_subtotal($noSPJ);
		$data['discount'] = $this->reser->get_discount($noSPJ);
		$data['chargeA'] = $this->reser->get_chargeA($noSPJ);
		$data['chargeB'] = $this->reser->get_chargeB($noSPJ);
		$data['biaya_awal'] = $this->reser->get_biaya_by_reservasi($noSPJ);
		$array = $this->reser->get_fee_driver($data['reservasi']['Asal'], $data['reservasi']['Tujuan']);
		$data['fee_driver'] = $array['fee'];
		$data['jumlah_seat'] = $this->reser->get_jumlah_seat($noSPJ);
		$data['jumlah_penumpang'] = $this->reser->get_jumlah_penumpang($noSPJ);
		$array = $this->pros->get_one_by_jurusan($data['reservasi']['Asal'],$data['reservasi']['Tujuan']);
                $data['charge_supir'] = $array['charge_supir'];
		$data['spbu'] = $this->spbu->get_all();
                $data['spbu_voucher'] = $this->spbu->get_one_by_nospj($noSPJ);
                $this->load->view('reservasi/cetak_op_awal',$data);
	}
	
	function list_setoran($tgl='') {
                if($this->session->userdata('level')!=4)
                {
                    $data['daftar_asal_tujuan'] = $this->reser->get_asal_tujuan();
                    $data['list'] = $this->reser->get_all_setoran($tgl);
                }
                else
                {
                    $data['daftar_asal_tujuan'] = $this->reser->get_asal_tujuan();
                    $data['daftar_tujuan'] = $this->reser->get_asal_tujuan_for_kasir($this->session->userdata('kota'));
                    $data['list'] = $this->reser->get_all_setoran_for_kasir_tujuan($this->session->userdata('kota'),$tgl);
                }
		if ($tgl == '') {
			$data['tgl'] = date('m/d/Y');
		} else {
			$data['tgl'] = $this->date_func->change_format_date($tgl, 'd-m-Y', 'm/d/Y');
		}

		$this->load->view('template/header_view');
		$this->load->view('reservasi/daftar_setoran', $data);
		$this->load->view('template/footer_view');
	}

	function list_setoran_bank($tgl='') {
                if($this->session->userdata('level')!=4)
                {
                    $data['daftar_asal_tujuan'] = $this->reser->get_asal_tujuan();
                    $data['list'] = $this->reser->get_all_setoran_bank($tgl);
                }else
                {
                    $data['daftar_asal_tujuan'] = $this->reser->get_asal_tujuan();
                    $data['daftar_tujuan'] = $this->reser->get_asal_tujuan_for_kasir($this->session->userdata('kota'));
                    $data['list'] = $this->reser->get_all_setoran_bank_for_kasir($this->session->userdata('kota'),$tgl);
                }
		for ($i = 0; $i < count($data['list']); $i++) {
			$data['join'][$i] = $this->reser->get_setoran_join_user($data['list'][$i]['id']);
		}
		if ($tgl == '') {
			$data['tgl'] = date('m/d/Y');
		} else {
			$data['tgl'] = $this->date_func->change_format_date($tgl, 'd-m-Y', 'm/d/Y');
		}

		$this->load->view('template/header_view');
		$this->load->view('reservasi/daftar_setoran_bank', $data);
		$this->load->view('template/footer_view');
	}

	function detail_setoran_bank($idsetoranBank, $tgl='') {
		$data['daftar_asal_tujuan'] = $this->reser->get_asal_tujuan();
		$data['list'] = $this->reser->get_all_setoran_join_user($idsetoranBank, $tgl);

		if ($tgl == '') {
			$data['tgl'] = date('m/d/Y');
		} else {
			$data['tgl'] = $this->date_func->change_format_date($tgl, 'd-m-Y', 'm/d/Y');
		}

		$this->load->view('reservasi/detail_setoran_bank', $data);
	}
	
	function cetak_setoran() {
		$this->load->view('reservasi/cetak_setoran_bank', $_GET);
	}

	
	
	function form_setoran($noSPJ) {
		$data['reservasi'] = $this->reser->get_one($noSPJ);
		$data['subtotal'] = $this->reser->get_subtotal($noSPJ);
		$data['discount'] = $this->reser->get_discount($noSPJ);
		$data['chargeA'] = $this->reser->get_chargeA($noSPJ);
		$data['chargeB'] = $this->reser->get_chargeB($noSPJ);
		$data['biaya_awal'] = $this->reser->get_biaya_by_reservasi($noSPJ);
		$array = $this->reser->get_fee_driver($data['reservasi']['Asal'], $data['reservasi']['Tujuan']);
		$data['fee_driver'] = $array['fee'];
		$data['jumlah_seat'] = $this->reser->get_jumlah_seat($noSPJ);
		$data['jumlah_penumpang'] = $this->reser->get_jumlah_penumpang($noSPJ);
		$array = $this->pros->get_one_by_jurusan($data['reservasi']['Asal'],$data['reservasi']['Tujuan']);
                $data['charge_supir'] = $array['charge_supir'];
		$data['spbu'] = $this->spbu->get_all();
                $data['spbu_voucher'] = $this->spbu->get_one_by_nospj($noSPJ);
		$this->load->view('reservasi/form_setoran', $data);
	}

	function form_setoran_awal($noSPJ) {
		$data['reservasi'] = $this->reser->get_one($noSPJ);
		$data['subtotal'] = $this->reser->get_subtotal($noSPJ);
		$data['discount'] = $this->reser->get_discount($noSPJ);
		$data['chargeA'] = $this->reser->get_chargeA($noSPJ);
		$data['chargeB'] = $this->reser->get_chargeB($noSPJ);
		$array = $this->reser->get_fee_driver($data['reservasi']['Asal'], $data['reservasi']['Tujuan']);
		$data['fee_driver'] = $array['fee'];
		$data['jumlah_seat'] = $this->reser->get_jumlah_seat($noSPJ);
		$data['jumlah_penumpang'] = $this->reser->get_jumlah_penumpang($noSPJ);
		$array = $this->pros->get_one_by_jurusan($data['reservasi']['Asal'],$data['reservasi']['Tujuan']);
                $data['charge_supir'] = $array['charge_supir'];
		$data['spbu'] = $this->spbu->get_all();
		$this->load->view('reservasi/form_setoran_awal', $data);
	}

	function form_setoran_bank() {
		$sum = 0;
		for ($i = 0; $i < count($_POST['spj_check']); $i++) {
			$array = $this->reser->get_one_setoran($_POST['spj_check'][$i]);
			$sum += $array['total_setoran'];
//			$this->reser->update_setoran($_POST['spj_check'][$i], array('is_closing' => 1));
		}
		$data['total'] = $sum;
		$data['spj_check'] = $_POST['spj_check'];
                $this->load->view('template/header_view');
		$this->load->view('reservasi/form_setoran_bank', $data);
                $this->load->view('template/footer_view');
	}

	function laporan_setoran_bank() {
		$data['reservasi'] = $this->reser->get_one($noSPJ);
		for ($i = 0; $i < count($_POST['spj_check']); $i++) {
			$data['join'][$i] = $this->reser->get_setoran_join_user($_POST['spj_check'][$i]);
//			$this->reser->update_setoran($_POST['spj_check'][$i], array('is_closing' => 1));
		}
		$data['spj_check'] = $_POST['spj_check'];
		$this->load->view('laporan/laporan_setoran_bank', $data);
	}

	function cetak($no_spj) {
		$data = $this->kasir->get_one($no_spj);
		if ($data) {
			$data['penjualan_kotor'] = $this->reser->get_subtotal($no_spj);
			
			$biaya = $this->biaya->get_biaya($no_spj);
			$data['total_biaya'] = 0;
			if ($biaya) {
				for ($i = 0; $i < count($biaya); $i++) {
					$data['list_biaya'][$biaya[$i]['nama_coa']] = $biaya[$i]['jumlah'];
					$data['list_kode'][$biaya[$i]['nama_coa']] = $biaya[$i]['kode_coa_biaya'];
					$data['total_biaya']+=$biaya[$i]['jumlah'];
				}
			}
                        $data['setoran'] = $this->reser->get_one_setoran($no_spj);
                        $data['penjualan_bersih'] = intval($data['penjualan_kotor']['SubTotal']) - intval($data['total_discount']) - intval($data['fee_agen'])-$data['setoran']['total_void'];
			$data['total_setor'] = $data['penjualan_bersih'] - $data['total_biaya'];
                        $data['spbu_voucher'] = $this->spbu->get_one_by_nospj($no_spj);
			$html = $this->load->view('reservasi/html_cetak', $data);
		} else {
			redirect(RESER . 'list_reservasi');
		}
	}

	function process_setoran_bank() {
		$data['noresi'] = $this->input->post('noresi');
		$data['tanggal'] = $this->date_func->change_format_date($this->input->post('tanggal'), 'm/d/Y', 'Ymd');
		$data['total'] = $this->input->post('total');
		$data['idkasir'] = $this->session->userdata('id');
		$this->reser->insert_setoran_bank($data);
		$id = $this->reser->get_max_id_setoran_bank();
		for ($i = 0; $i < count($_POST['spj_check']); $i++) {
			$this->reser->update_setoran($_POST['spj_check'][$i], array('is_closing' => 1, 'idsetoranBank' => $id['id']));
		}
		redirect(RESER . 'list_setoran');
	}

	function process_setoran() {
		//setoran
		$setoran['NoSPJ'] = $this->input->post('NoSPJ');
                $data['biaya_awal'] = $this->reser->get_biaya_by_reservasi($setoran['NoSPJ']);
		$setoran['asal'] = $this->input->post('asal');
		$setoran['NoPol'] = $this->input->post('NoPol');
		$setoran['driver'] = $this->input->post('driver');
		$setoran['tujuan'] = $this->input->post('tujuan');
		$setoran['tanggal'] = $this->date_func->change_format_date($this->input->post('tanggal'), 'd-m-Y', 'Ymd');
		$setoran['total_kapasitas'] = $this->input->post('total_kapasitas');
		$setoran['total_penumpang'] = $this->input->post('total_penumpang');
		$setoran['idKasirSetoran'] = $this->session->userdata('id');
		$setoran['total_setoran'] = $this->input->post('total_setoran');
                $setoran['total_laba'] = $setoran['total_setoran'];
                $array = $this->reser->get_biaya_by_reservasi($setoran['NoSPJ']);
                for($i=0;$i<count($array);$i++)
                    $setoran['total_laba'] += $array[$i]['jumlah'];
		$setoran['fee_driver'] = $this->input->post('fee_driver');
		$setoran['total_charge'] = $this->input->post('chargeA') + $this->input->post('chargeB');
		$setoran['total_kapasitas'] = $this->input->post('total_kapasitas');
		$setoran['total_penumpang'] = $this->input->post('total_penumpang');
		$setoran['total_omset'] = $this->input->post('penjualan_kotor');
		$setoran['total_discount'] = $this->input->post('total_discount');
		$setoran['total_void'] = $this->input->post('total_void');
		$setoran['total_biaya_langsung'] = $this->input->post('total_biaya_langsung');
		$setoran['total_laba'] = $this->input->post('total_penjualan_bersih');
		$setoran['is_closing'] = 0;
		$setoran['tiket_void'] = $this->input->post('tiket_void');
		$setoran['fee_agen'] = $this->input->post('fee_agen');
		$setoran['tglspj'] = $this->input->post('tglspj');
		$setoran['cabang_keberangkatan'] = $this->input->post('cabang_keberangkatan');
		$this->reser->insert_setoran($setoran);
		//biaya
		$biaya['NoSPJ'] = $this->input->post('NoSPJ');
		$biaya['idbiaya'] = 1;
		$biaya['jumlah'] = $this->input->post('fee_driver');

		$biaya['is_tujuan'] = 1;
		$biaya['tgl_pembayaran'] = date('Ymd');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
		$biaya['idkasir'] = $this->session->userdata('id');
		$biaya['cabang'] = $setoran['cabang_keberangkatan'];
		$biaya['tgl_pembayaran'] = $this->date_func->change_format_date($this->input->post('tanggal'), 'd-m-Y', 'Ymd');
//                if (count($data['biaya_awal'])>0)
//                {
//                    echo $this->reser->update_biaya(($data['biaya_awal'][0]['id']),$biaya);
//                }else 
                    $this->reser->insert_biaya($biaya);
                
		$biaya['idbiaya'] = 2;
		$biaya['jumlah'] = $this->input->post('perpal');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
//		if (count($data['biaya_awal'])>0)
//                {
//                    echo $this->reser->update_biaya(($data['biaya_awal'][1]['id']),$biaya);
//                }else 
                    $this->reser->insert_biaya($biaya);

		$biaya['idbiaya'] = 3;
		$biaya['jumlah'] = $this->input->post('bbm_cash');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
//		if (count($data['biaya_awal'])>0)
//                {
//                    echo $this->reser->update_biaya(($data['biaya_awal'][2]['id']),$biaya);
//                }else 
                    $this->reser->insert_biaya($biaya);
                
		$biaya['idbiaya'] = 4;
		$biaya['jumlah'] = $this->input->post('parkirtol');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
//		if (count($data['biaya_awal'])>0)
//                {
//                    echo $this->reser->update_biaya(($data['biaya_awal'][3]['id']),$biaya);
//                }else 
                    $this->reser->insert_biaya($biaya);
                
		$biaya['idbiaya'] = 5;
		$biaya['jumlah'] = $this->input->post('ferry_penyebrangan');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
//		if (count($data['biaya_awal'])>0)
//                {
//                    echo $this->reser->update_biaya(($data['biaya_awal'][4]['id']),$biaya);
//                }else 
                    $this->reser->insert_biaya($biaya);
                
		$biaya['idbiaya'] = 6;
		$biaya['jumlah'] = $this->input->post('makan_penumpang');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
//		if (count($data['biaya_awal'])>0)
//                {
//                    echo $this->reser->update_biaya(($data['biaya_awal'][5]['id']),$biaya);
//                }else 
                    $this->reser->insert_biaya($biaya);
                
		$biaya['idbiaya'] = 7;
		$biaya['jumlah'] = $this->input->post('snack_penumpang');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
//		if (count($data['biaya_awal'])>0)
//                {
//                    echo $this->reser->update_biaya(($data['biaya_awal'][6]['id']),$biaya);
//                }else 
                    $this->reser->insert_biaya($biaya);
                
		$biaya['idbiaya'] = 8;
		$biaya['jumlah'] = $this->input->post('koordinasi_lapangan');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
//		if (count($data['biaya_awal'])>0)
//                {
//                    echo $this->reser->update_biaya(($data['biaya_awal'][7]['id']),$biaya);
//                }else 
                    $this->reser->insert_biaya($biaya);
                
		//spbu voucher
		if ($this->input->post('jenis_bbm') == "voucher") {
                        if($this->reser->sudah_spbu_voucher($this->input->post('NoSPJ')))
                        {
                            $array = $this->reser->get_max_id_setoran();
                            $spbu['idspbu'] = $this->input->post('idspbu');
                            $spbu['kode_voucher'] = $this->input->post('kode_voucher');
                            $spbu['jenis_bbm'] = $this->input->post('tipe_bbm');
                            $spbu['jumlah_liter'] = $this->input->post('jumlah_liter');
                            $spbu['jumlah_uang'] = $this->input->post('jumlah_uang');
                            $this->reser->update_spbu_voucher($this->input->post('NoSPJ'),$spbu);
                        }
                        else
                        {
                            $array = $this->reser->get_max_id_setoran();
                            $spbu['NoSPJ'] = $this->input->post('NoSPJ');
                            $spbu['idspbu'] = $this->input->post('idspbu');
                            $spbu['kode_voucher'] = $this->input->post('kode_voucher');
                            $spbu['jenis_bbm'] = $this->input->post('tipe_bbm');
                            $spbu['jumlah_liter'] = $this->input->post('jumlah_liter');
                            $spbu['jumlah_uang'] = $this->input->post('jumlah_uang');
                            $this->reser->insert_spbu_voucher($spbu);
                        }
		}
		redirect(RESER . 'list_setoran');
	}

	function process_setoran_awal() {
                $setoran['cabang_keberangkatan'] = $this->input->post('cabang_keberangkatan');
		//biaya
		$biaya['NoSPJ'] = $this->input->post('NoSPJ');
		$biaya['idbiaya'] = 2;
		$biaya['jumlah'] = $this->input->post('perpal');
		$biaya['is_tujuan'] = 0;
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
		$biaya['idkasir'] = $this->session->userdata('id');
		$biaya['cabang'] = $setoran['cabang_keberangkatan'];
		$biaya['tgl_pembayaran'] = $this->date_func->change_format_date($this->input->post('tanggal'),'d-m-Y','Ymd');
		$this->reser->insert_biaya($biaya);
		$biaya['idbiaya'] = 3;
		$biaya['jumlah'] = $this->input->post('bbm_cash');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
		$this->reser->insert_biaya($biaya);
		$biaya['idbiaya'] = 4;
		$biaya['jumlah'] = $this->input->post('parkirtol');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
		$this->reser->insert_biaya($biaya);
		$biaya['idbiaya'] = 5;
		$biaya['jumlah'] = $this->input->post('ferry_penyebrangan');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
		$this->reser->insert_biaya($biaya);
		$biaya['idbiaya'] = 6;
		$biaya['jumlah'] = $this->input->post('makan_penumpang');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
		$this->reser->insert_biaya($biaya);
		$biaya['idbiaya'] = 7;
		$biaya['jumlah'] = $this->input->post('snack_penumpang');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
		$this->reser->insert_biaya($biaya);
		$biaya['idbiaya'] = 8;
		$biaya['jumlah'] = $this->input->post('koordinasi_lapangan');
		$array = $this->coa->get_one($biaya['idbiaya']);
		$biaya['kode_coa_biaya'] = $array['kode_coa'];
		$biaya['nama_coa'] = $array['nama_coa'];
		$this->reser->insert_biaya($biaya);
                if ($this->input->post('jenis_bbm') == "voucher") {
			$array = $this->reser->get_max_id_setoran();
			$spbu['NoSPJ'] = $this->input->post('NoSPJ');
			$spbu['idspbu'] = $this->input->post('idspbu');
			$spbu['kode_voucher'] = $this->input->post('kode_voucher');
			$spbu['jenis_bbm'] = $this->input->post('tipe_bbm');
			$spbu['jumlah_liter'] = $this->input->post('jumlah_liter');
			$spbu['jumlah_uang'] = $this->input->post('jumlah_uang');
			$this->reser->insert_spbu_voucher($spbu);
		}
		redirect(RESER.'list_pembayaran_operasional_awal');
	}
	
}

/* End of file home.php */
/* Location: ./application/controllers/home.php */

